﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OnlineCourse.Model;
using OnlineCourse.Model.Entity;
using OnlineCourse.Services.Model;

namespace OnlineCourse.Services
{
    public class CourseService : IServices.ICourseServices
    {
        private readonly ApplicationDbContext _db;
        public CourseService(ApplicationDbContext db)
        {
            this._db = db;
        }
        /// <summary>
        /// 添加课程相关信息
        /// </summary>
        /// <param name="teacherId"></param>
        /// <param name="title"></param>
        /// <param name="type"></param>
        /// <param name="imagePath"></param>
        /// <param name="detailed"></param>
        /// <param name="price"></param>
        /// <param name="buyNum"></param>
        /// <param name="lessonHour"></param>
        /// <returns></returns>
        public bool Add( Guid teacherId,string title, int type, string imagePath, string detailed, decimal price, string lessonHour)
        {
            try
            {
                Course model = new Course();
                model.TeacherId = teacherId;
                model.Title = title;
                model.Type = type;
                model.ImagePath = imagePath;
                model.Detailed = detailed;
                model.Price = price;
                model.BuyNum = 0;
                model.LessonHour = lessonHour;
                this._db.Courses.Add(model);
                _db.SaveChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }
        /// <summary>
        /// 更新课程信息
        /// </summary>
        /// <param name="id"></param>
        /// <param name="title"></param>
        /// <param name="type"></param>
        /// <param name="imagePath"></param>
        /// <param name="detailed"></param>
        /// <param name="price"></param>
        /// <param name="buyNum"></param>
        /// <param name="lessonHour"></param>
        /// <returns></returns>
        public bool Update(Guid id, string title, int type, string imagePath, string detailed, decimal price, string lessonHour)
        {
            try
            {
                var m = _db.Courses.FirstOrDefault(p => p.Id == id);
                if (m != null)
                {
                    m.Title = title;
                    m.Type = type;
                    m.ImagePath = imagePath;
                    m.Detailed = detailed;
                    m.Price = price;
                    m.LessonHour = lessonHour;
                    _db.SaveChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        /// <summary>
        /// 删除课程信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(Guid id)
        {
            try
            {
                var m = _db.Courses.FirstOrDefault(p => p.Id == id);
                _db.Courses.Remove(m);
                _db.SaveChanges();
                return true;
            }
            catch (Exception err)
            {
                throw err;
            }
        }
        /// <summary>
        /// 按课程类型分页查询
        /// </summary>
        /// <param name="Type"></param>
        /// <param name="total"></param>
        /// <param name="page"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public IList<CourseModel> GetCourseList(int Type,out int total, int page , int pageSize )
        {
            var list = _db.Courses.Where(b => b.Type == Type).Select(a => (new CourseModel() { Id = a.Id, TeacherName = a.Teacher.Name, Title = a.Title, ImagePath = a.ImagePath, Detailed = a.Detailed, Price = a.Price, BuyNum = a.BuyNum, LessonHour = a.LessonHour }));
            var list1 = list.Skip(pageSize * (page - 1)).Take(pageSize).ToList();
            total = list1.Count();
            return list1;
        }
        /// <summary>
        /// 无条件查询按老师排序
        /// </summary>
        /// <returns></returns>
        public List<Course> GetList()
        {
            var list= _db.Courses.OrderBy(p => p.Teacher.Name).Skip(0).ToList();
            return list;
        }
        /// <summary>
        /// 单个课程详细内容查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public CourseModel GetCourse(Guid id)
        {
            CourseModel model = new CourseModel();
            model = _db.Courses.Where(a => a.Id == id).Select(a => (new CourseModel() { Id = a.Id,TeacherName =a.Teacher.Name, Title = a.Title, ImagePath = a.ImagePath, Detailed = a.Detailed, Price = a.Price, BuyNum = a.BuyNum, LessonHour = a.LessonHour })).FirstOrDefault();
            return model;
        }
    }
}